package yxy.game.pm2.module.therionHunting;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;

import java.util.Map;


/**
 * @author wrs
 * @name ChallengingPosition
 * @description 神兽玩法  挑战位信息模型
 * @since 2022-06-09
 */
public class TherionChallengingPosition extends BaseGameRedisLockBean {

    // bean==========================================================================

    private int position; //挑战位置
    private String playCode; //
    private DateTime time; //占位开始时间,用于计算伤害值
    private DateTime occupyTime; //用于计算占位时长

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getPlayCode() {
        return playCode;
    }

    public void setPlayCode(String playCode) {
        this.playCode = playCode;
    }

    public DateTime getTime() {
        return time;
    }

    public void setTime(DateTime time) {
        this.time = time;
    }

    public DateTime getOccupyTime() {
        return occupyTime;
    }

    public void setOccupyTime(DateTime occupyTime) {
        this.occupyTime = occupyTime;
    }

    // func===========================================================================

    //初始化,挑战位上为空
    private static void create(int type,RMap<Integer, TherionChallengingPosition> rmap) {
        if (type == TherionModule.THERION_LOCAL || type == TherionModule.THERION_CROSS){
            for (int i= 101; i <= 106 ;i++){
                TherionChallengingPosition challengingPosition = new TherionChallengingPosition();
                challengingPosition.setPosition(i);
                challengingPosition.setPlayCode(null);
                challengingPosition.setTime(DateTime.now());
                challengingPosition.setOccupyTime(DateTime.now());
                rmap.put(i,challengingPosition);
            }

        }else if (type == TherionModule.THERION_WORLD){
            TherionChallengingPosition challengingPosition = new TherionChallengingPosition();
            challengingPosition.setPosition(101);
            challengingPosition.setPlayCode(null);
            challengingPosition.setTime(DateTime.now());
            challengingPosition.setOccupyTime(DateTime.now());
            rmap.put(challengingPosition.getPosition(),challengingPosition);
        }
    }

    // redis==========================================================================

    /**
     *
     * @param gameType 神兽玩法模式
     * @param playerCode
     * @return
     */
    static public String key(int gameType, String playerCode,int therionType) {
        PlayerBean playerBean = PlayerBean.get(playerCode);
        ServerBean serverBean = ServerBean.get(playerBean.getServerId());
        AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
        if (gameType == TherionModule.THERION_LOCAL) {
            return String.format("THERION:AREA:%s:THERIONCHALLENGING:%s", serverBean.getAreaId(),therionType);
        }

        if (gameType == TherionModule.THERION_CROSS) {
            return String.format("THERION:AREAGROUP:%s:THERIONCHALLENGING:%s", areaBean.getGroupId(),therionType);
        }

        if (gameType == TherionModule.THERION_WORLD) {
            return String.format("THERION:WORLD:%s:THERIONCHALLENGING:%s",areaBean.getGroupId(),therionType);
        }
        return null;
    }

    static private RMap<Integer, TherionChallengingPosition> touchMap(int type, String playerCode,int therionType) throws RedisException {
        return RedisManager.client(redis()).getMap(key(type, playerCode,therionType));
    }

    public void set(int gameType,String playerCode,int therionType,int position) {
        try {
            RMap<Integer, TherionChallengingPosition> map = touchMap(gameType, playerCode,therionType);
            map.put(position, this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    //获取全部
    static public Map<Integer, TherionChallengingPosition> getAll(int gameType, String playCode,int therionType) {
        try {
            RMap<Integer, TherionChallengingPosition> rmap = touchMap(gameType, playCode,therionType);
            //初始化6个位置
            if (rmap.size() <= 0) {
                create(gameType,rmap);
            }
            return rmap.readAllMap();
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取某个位置
    static public TherionChallengingPosition get(int gameType, String playCode, int therionType,int position) {
        try {
            RMap<Integer, TherionChallengingPosition> rmap = touchMap(gameType, playCode,therionType);
            //初始化6个位置
            if (rmap.size() <= 0) {
                create(gameType,rmap);
            }
            return rmap.readAllMap().get(position);
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    //红点
    private void check() {

    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(int gameType,String playCode,int therionType) {
        try {
            touchMap(gameType,playCode,therionType).delete();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //定时任务---------------------------------------------------------------------------

    static public String key(int gameType, int areaIdOrGroupId,int therionType) {
        if (gameType == TherionModule.THERION_LOCAL) {
            return String.format("THERION:AREA:%s:THERIONCHALLENGING:%s", areaIdOrGroupId,therionType);
        }

        if (gameType == TherionModule.THERION_CROSS) {
            return String.format("THERION:AREAGROUP:%s:THERIONCHALLENGING:%s", areaIdOrGroupId,therionType);
        }

        if (gameType == TherionModule.THERION_WORLD) {
            return String.format("THERION:WORLD:%s:THERIONCHALLENGING:%s",areaIdOrGroupId,therionType);
        }
        return null;
    }

    static private RMap<Integer, TherionChallengingPosition> touchMap(int type, int areaIdOrGroupId,int therionType) throws RedisException {
        return RedisManager.client(redis()).getMap(key(type, areaIdOrGroupId,therionType));
    }

    //获取某个位置
    static public TherionChallengingPosition get(int gameType, int areaIdOrGroupId, int therionType,int position) {
        try {
            RMap<Integer, TherionChallengingPosition> rmap = touchMap(gameType, areaIdOrGroupId,therionType);
            //初始化6个位置
            if (rmap.size() <= 0) {
                create(gameType,rmap);
            }
            return rmap.readAllMap().get(position);
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取全部位置
    static public Map<Integer, TherionChallengingPosition> getAll(int gameType, int areaIdOrGroupId,int therionType) {
        try {
            RMap<Integer, TherionChallengingPosition> rmap = touchMap(gameType, areaIdOrGroupId,therionType);
            //初始化6个位置
            if (rmap.size() <= 0) {
                create(gameType,rmap);
            }
            return rmap.readAllMap();
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(int gameType,int areaIdOrGroupId,int therionType) {
        try {
            touchMap(gameType,areaIdOrGroupId,therionType).delete();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // lock==========================================================================
    static private String lockKey(int type,String playerCode,int therionType) {
        return String.format("lock:%s", key(type,playerCode,therionType));
    }

    static public boolean lockTherionChallengingPosition(int type,String playerCode,int therionType) throws InterruptedException {
        return tryLock(lockKey(type,playerCode,therionType));
    }

    static public void unlockTherionChallengingPosition(int type,String playerCode,int therionType) {
        unlock(lockKey(type,playerCode,therionType));
    }
}
